//
//  WBNewFatureView.swift
//  GitWeibo
//
//  Created by 胡金东 on 2017/6/1.
//  Copyright © 2017年 胡金东. All rights reserved.
//

import UIKit

class WBNewFatureView: UIView {

    @IBOutlet var scroll: UIScrollView!
    @IBOutlet var enterButton: UIButton!
    @IBOutlet var pageControl: UIPageControl!
    
    @IBAction func enterButtonClick() {
        removeFromSuperview()
    }
    
    class func newFature() -> WBNewFatureView {
        let nib = UINib(nibName: "WBNewFatureView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0] as! WBNewFatureView
        
        v.frame = UIScreen.main.bounds
        
        return v
    }
    
    override func awakeFromNib() {
        let count = 4
        let rect = UIScreen.main.bounds
        
        for i in 0..<count {
            let imageName = "new_feature_\(i+1)"
            let imageView = UIImageView(image: UIImage(named: imageName))
            
            imageView.frame = rect.offsetBy(dx: CGFloat(i) * rect.width, dy: 0)
            
            scroll.addSubview(imageView)
            
            
        }
      
        scroll.contentSize = CGSize(width: CGFloat(count) * rect.width, height: rect.height)
        scroll.bounces = false
        scroll.isPagingEnabled = true
        scroll.showsVerticalScrollIndicator = false
        scroll.showsHorizontalScrollIndicator = false
        scroll.delegate = self
        /// - 不显示button
        enterButton.isHidden = true
        pageControl.isUserInteractionEnabled = false
        
    }
    
   }


extension WBNewFatureView:UIScrollViewDelegate{
    /// - 滑动减速时候
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        /// - 获取那一页
        let page = Int(scroll.contentOffset.x / scroll.bounds.width)
        /// - 如果是最后一页，就不显示button
        enterButton.isHidden = (page != scroll.subviews.count - 1 )
    }
    /// - 滑动就调用
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        /// - 滑动就隐藏button
        enterButton.isHidden = true
        /// - 计算当前偏移量
        let currentPage = Int(scroll.contentOffset.x / scroll.bounds.width + 0.5)
        /// - 设置分页数
        pageControl.currentPage = currentPage
        
        pageControl.isHidden = (currentPage == scroll.subviews.count)
        
    }
}
